/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.uml2.uml.Port;

import com.cea.papyrus.classdiagram.parsers.texteditor.portlabel.PortLabelSourceViewerConfiguration;

/**
 * 
 */
public class PortLabelEditorDialog extends LabelEditorDialog {

	/**
	 * 
	 */
	final private static String TITLE = "Port";
	
	/**
	 * 
	 */
	private Port port;
	
	/**
	 * 
	 */
	private Document portLabelDocument;
	
	/**
	 * 
	 */
	private String value;
	
	/**
	 * 
	 * 
	 * @param parentShell 
	 * @param port 
	 * @param initialValue 
	 */
	public PortLabelEditorDialog(Shell parentShell, Port port, String initialValue) {
		super(parentShell, TITLE, initialValue, new PortLabelValidator(port));
		this.port = port;
		this.value = initialValue;
	}
	
    /* (non-Javadoc)
     * @see com.cea.papyrus.core.utils.dialog.LabelEditorDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    /**
     * 
     * 
     * @param parent 
     * 
     * @return 
     */
    @Override
	protected Control createDialogArea(Composite parent) {
        Group composite = new Group(parent, SWT.RESIZE);
        composite.setText("Specification");
        GridLayout layout = new GridLayout();
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        
        Composite viewerGroup= new Composite(composite, SWT.RESIZE); 
        FillLayout viewerLayout = new FillLayout();
        viewerGroup.setLayout(viewerLayout);
        GridData data = new GridData(GridData.GRAB_HORIZONTAL
              | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
              | GridData.VERTICAL_ALIGN_CENTER);
        data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);      
        viewerGroup.setLayoutData(data);
        viewer = new SourceViewer(viewerGroup, null, SWT.BORDER | SWT.FILL_EVEN_ODD );
        
        // configure source viewer
        portLabelDocument = new Document();
        portLabelDocument.set(value);
       
       // add completion processor key listener (ctrl+space keys) 
        viewer.appendVerifyKeyListener(new PortLabelKeyListener(viewer));
        
        portLabelDocument.addDocumentListener(new DocumentListener());
        PortLabelSourceViewerConfiguration config = new PortLabelSourceViewerConfiguration();
        config.setPort(port);
        viewer.configure(config);
        viewer.setDocument(portLabelDocument);
        
        viewer.setSelectedRange(0, value.length());
        errorMessageText = new Text(composite, SWT.READ_ONLY);
        errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
                | GridData.HORIZONTAL_ALIGN_FILL));
        errorMessageText.setBackground(errorMessageText.getDisplay()
                .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        // Set the error message text
        // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292
        setErrorMessage(errorMessage);

        applyDialogFont(composite);
        return composite;
    }

    /**
     * 
     */
    private class PortLabelKeyListener implements VerifyKeyListener {
        
        /**
         * 
         */
        SourceViewer viewer;
        
        /**
         * Default constructor.
         * 
         * @param viewer 
         */
        public PortLabelKeyListener(SourceViewer viewer) {
            this.viewer = viewer;
        }
        
        /* (non-Javadoc)
         * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
         */
        /**
         * 
         * 
         * @param event 
         */
        public void verifyKey(VerifyEvent event) {
            if ((event.stateMask == SWT.CTRL) && (event.character == ' ')) {
            	if (viewer.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS)) {
                    viewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
                }
                event.doit = false;
            } else if(event.character == SWT.CR) {
                event.doit=false;
            } 
        }
    }
    
    /**
     * 
     */
    private class DocumentListener implements IDocumentListener {

		/**
		 * 
		 * 
		 * @param event 
		 */
		public void documentAboutToBeChanged(DocumentEvent event) {
		}

		/**
		 * 
		 * 
		 * @param event 
		 */
		public void documentChanged(DocumentEvent event) {
			validateInput();
		}
    	
    }
    
}
